﻿using System.Collections.ObjectModel;
using System.Diagnostics;

namespace NConfigGenerator
{
    [DebuggerDisplay("{Name} : {Type}")]
    public sealed class ConfigNodeAttribute
    {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string _name;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private AttributeType _type;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private bool _optional;

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private bool _isKey;

        public ConfigNodeAttribute(string name, AttributeType type)
        {
            _name = name;
            _type = type;
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public AttributeType Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public bool Optional
        {
            get { return _optional; }
            set { _optional = value; }
        }

        public bool IsKey
        {
            get { return _isKey; }
            set { _isKey = value; }
        }
    }

    public sealed class ConfigNodeAttributeCollection : KeyedCollection<string, ConfigNodeAttribute>
    {
        internal ConfigNodeAttributeCollection()
        {
        }

        internal string KeyName
        {
            get
            {
                if (Count == 0)
                    return null;
                foreach (ConfigNodeAttribute attribute in this)
                {
                    if (attribute.IsKey)
                        return attribute.Name;
                }
                return this[0].Name;
            }
        }

        protected override string GetKeyForItem(ConfigNodeAttribute item)
        {
            return item.Name;
        }
    }
}